package com.jvm123.ygdy

import groovy.sql.Sql

/**
 *
 * @author yawn
 * @date 2021/1/19 星期二
 *
 */

// sql db
url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8&useUnicode=true"
sql = Sql.newInstance(url, "root", "root", "com.mysql.cj.jdbc.Driver")




// file
FileReader fr = new FileReader("s.ygdy8.com.txt")
BufferedReader br = new BufferedReader(fr)
String line
while ((line = br.readLine()) != null) {
    def name = getName(line)
    def title = getTitle(line)
    def href = getHref(line)
    def time = getTime(line)
    def des = getDes(line)

    if (name == null) {
        continue
    }

    def insertSql = 'INSERT INTO movie (name, title, href, time, des) VALUES (?, ?, ?, ?, ?)'
    def params = [name, title, href, time, des]
    def keys = sql.executeInsert(insertSql, params)
    println keys
    println name
//    println line
}

sql.close()

def getName(String line) {
    String[] s1s = line.split("《")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("》")
        return s2s[0]
    }
}

def getTitle(String line) {
    String[] s1s = line.split("\"red\">")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("</a>")
        return s2s[0].replace("</font>", "")
    }
}

def getHref(String line) {
    String[] s1s = line.split("\"")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("\"")
        return s2s[0]
    }
}

def getTime(String line) {
    String[] s1s = line.split("8F8C89\">")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("<")
        return s2s[0].replace("(", "").replace(")", "")
    }
}

def getDes(String line) {
    String[] s1s = line.split("colspan=\"3\">")
    if (s1s.length >= 2) {
        String[] s2s = s1s[1].split("<font")
        return s2s[0]
    }
}